<template>
  <div>
    <Row class="header" type="flex" justify="center">
      <Col span="14">
        <Icon class="icon" type="ios-podium-outline"/>
        <span class="title">各类型材料数量统计</span>
      </Col>
      <Col span="10">
        <Select @on-change="change" v-model="model1" v-if="cityList.length>0" filterable style="width:100%">
          <Option v-for="(item,index) in cityList" :value="item.value" :key="index">{{ item.label }}</Option>
        </Select>
      </Col>
    </Row>
    <br>
    <div id="TypesPillar" :style="{ width: width + 'px', height: height + 'px'}"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
// import { getMaterialTypeNum } from '@/api/statistics'
import { findCompanyList, materialsReport } from '@/api/record'
// import mockData from './mockData'

export default {
  name: 'TypesPillarEchart',
  data () {
    return {
      chartData: [],
      cityList: [
      ],
      model1: '全部'
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
    this.chart()
    this.init()
  },
  methods: {
    change () {
      console.log(this.model1)
      if (this.model1 === '全部') {
        this.chart()
      } else {
        this.chart(this.model1)
      }
    },
    init () {
      findCompanyList({ company_type: '1' }).then(ret => {
        if (ret.data.errcode === 0) {
          let dataList = ret.data.data
          let newArray = [{ label: '全部', value: '全部' }]
          for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].code) {
              newArray.push({
                label: dataList[i].name,
                value: dataList[i].code
              })
            }
          }
          this.cityList = newArray
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    },
    chart (data) {
      materialsReport({ company_code: data }).then(ret => {
        if (ret.data.errcode === 0) {
          // this.chartData = ret.data.data
          let zs = 0
          let youcailiao = 0
          let tupianbuquan = 0
          let quanbuhege = 0
          for (let i = 0; i < ret.data.data.length; i++) {
            if (ret.data.data[i].type === '0') {
              youcailiao = ret.data.data[i].total_count
            }
            if (ret.data.data[i].type === '1') {
              tupianbuquan = ret.data.data[i].total_count
            }
            if (ret.data.data[i].type === '2') {
              quanbuhege = ret.data.data[i].total_count
            }
          }
          zs = quanbuhege + tupianbuquan + youcailiao
          // 数据处理
          let arr = []
          arr.push({ name: '总量', num: zs })
          arr.push({ name: '有材料', num: youcailiao })
          arr.push({ name: '有材料但图片不全', num: tupianbuquan })
          arr.push({ name: '全部合格', num: quanbuhege })
          this.chartData = arr
          let option = {
            title: {
              text: ''
            },
            textStyle: {
              color: '#bababa'
            },
            // legend: {
            // left: '20%',
            // itemHeight: 10,
            // padding: [0, 0, 10, 0]
            // },
            tooltip: {
              trigger: 'axis',
              backgroundColor: 'rgba(50,50,50,0.6)',
              axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            dataset: {
              dimensions: ['name', 'num'],
              source: []
            },
            yAxis: {
              name: '单位：件',
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: '#bababa' // 轴及其文字颜色
                }
              }
            },
            xAxis: {
              type: 'category',
              axisLabel: {
                interval: 0,
                rotate: 15
              },
              axisLine: {
                lineStyle: {
                  color: '#bababa' // 轴及其文字颜色
                }
              }
            },
            series: [
              {
                name: '数量',
                type: 'bar',
                stack: '',
                barGap: 0,
                label: {
                  normal: {
                    show: true,
                    position: 'inside',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value.totalP > 0) {
                        return params.value.totalP
                      } else {
                        return ''
                      }
                    }
                  }
                },
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#61A0A9']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
            option.dataset.source.push(item)
          })
          let myChart = echarts.init(document.getElementById('TypesPillar'))
          myChart.setOption(option)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
    color: #333333;
  }
</style>
